import json

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View
from rest_framework import status
from rest_framework.generics import GenericAPIView, CreateAPIView, ListAPIView, \
    ListCreateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ReadOnlyModelViewSet

from .models import Spit, SpitComment
from . import serializers
from .pagination import SpitResultsSetPagination


class SpitListView(ListAPIView):
    queryset = Spit.objects.all()
    serializer_class = serializers.SpitListSerializer


class SpitDetailView(GenericAPIView):
    queryset = Spit.objects.all().order_by("-create_time")
    serializer_class = serializers.SpitDetailSerializer

    def get(self, request, pk):
        spit = self.get_object()
        # print(spit)
        s = self.get_serializer(spit)
        return Response(s.data)


class SpitCommentView(GenericAPIView):
    queryset = SpitComment.objects.all()
    serializer_class = serializers.SubSpitCommentSerializer

    # def get(self, request, pk):
    #     # queryset = SpitComment.objects.filter(spit_id=pk).order_by(
    #     #     "-create_time")
    #     # s = SpitCommentSerializer(queryset, many=True)
    #     #
    #     # return Response(s.data)
    #     spit_comment = self.get_object()
    #     print(spit_comment)
    #     s = self.get_serializer(spit_comment)
    #     print(s.data)
    #     return Response(s.data)

    # def post(self, request, pk):
    #     user = request.user
    #     if not user:
    #         return Response({"message": "请登录后评论"})
    #     # 获取数据
    #     data = self.request.data
    #     s = self.get_serializer(data=data)
    #     s.is_valid(raise_exception=True)
    #
    #     s.save()
    #     return Response(s.validated_data)


# class SpitListDetailViewSet(ReadOnlyModelViewSet):
#     """获取吐槽列表"""
#     queryset = Spit.objects.all().order_by("-create_time")
#
#     # 分页处理
#     # pagination_class = SpitResultsSetPagination
#
#     def get_serializer_class(self):
#         if self.action == "list":
#             return serializers.SpitListSerializer
#         else:
#             return serializers.SpitDetailSerializer

#
# class SpitLikeView(GenericAPIView):
#     serializer_class = serializers.SpitLikeSerializer
#     permission_classes = [IsAuthenticated]
#
#     def put(self, request, pk):
#         spit = Spit.objects.get(pk=pk)


class SpitReleaseView(CreateAPIView):
    """发布吐槽"""
    serializer_class = serializers.SpitReleaseSerializer

    permission_classes = [IsAuthenticated]

    # def post(self, request, *args, **kwargs):
    #     user = request.user
    #     if not user:
    #         return Response({"message": "请登录后发布"})
    #
    #     # spit = Spit()
    #
    #     content = request.data["content"]
    #     user_id = request.data["user_id"]
    #
    #     if not content:
    #         return Response({"message": "发布内容不能为空"})
    #
    #     s = self.get_serializer(data=content)
    #     s.is_valid(raise_exception=True)
    #     s.save()
    #     print(s.validated_data)
    #     return Response(s.validated_data)


# class SpitReleaseView(View):
#     def post(self, request):
#         json_bytes = request.body
#         print(json_bytes)
#         json_str = json_bytes.decode()
#         json_spit = json.loads(json_str)
#
#         spit = Spit.objects.create(
#             user=json_spit.get("user_id"),
#             content=json_spit.get("content")
#         )
#         return JsonResponse({
#             "id": spit.id,
#             "content": spit.content,
#             "user": spit.user_id,
#             "like_counts": spit.like_counts,
#             "collection_counts": spit.collection_counts,
#             "comment_counts": spit.comment_counts,
#             "share_counts": spit.share_counts
#         }, status=201)






